﻿// ===============================================================================
// Login.cs
// EOS Web Framework - Demo
// ===============================================================================
// Copyright (c) EOS Development Group. All rights reserved.
// ===============================================================================

namespace NewsDemo.Pages
{
    using EOS.Framework.Web.Mvc;
    using EOS.Framework.Web.Mvc.Controls;
    using Microsoft.Practices.Unity;
    using NewsDemo.Data;

    public class Login : WebPage
    {
        #region Dependencies

        [Dependency]
        public IEditorDao EditorDao { get; set; }
        
        #endregion

        class LoginForm : Form<Login>
        {
            protected override void BeginRender()
            {
                Textbox<string> usernameTextBox = new Textbox<string>(this);
                Attach("username", usernameTextBox);

                Textbox<string> passwordTextBox = new Textbox<string>(this);
                Attach("password", passwordTextBox);

                Checkbox stayLoggedInCheckbox = new Checkbox(this);
                Attach("stayLoggedIn", stayLoggedInCheckbox);

                if (Submitted)
                {
                    Editor currentEditor = null;
                    if ((currentEditor = Container.EditorDao.FindByNameAndPassword(usernameTextBox.Value, passwordTextBox.Value)) != null)
                    {
                        // User found for the entered username and password.
                        //WebContext.Current.Login(currentEditor, true);

                        // Refreshs the page by making a redirect to the current home page.
                        //Container.Context.HttpContext.Response.Redirect("~/");
                    }
                }
                else
                {
                    // If the form is not submitted, we hide the 
                    // error message.
                    Hide("error");
                }
            }
        }

        protected override void BeginRender()
        {
            if (WebContext.Current.Editor == null)
            {
                Hide("logoutBox");
                Attach("form", new LoginForm());
            }
            else
            {
                Hide("formBox");   
            }            
        }

    }
}
